package com.micro.ai.auth.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.micro.ai.auth.entity.DictType;
import com.micro.ai.auth.service.DictTypeService;
import com.micro.ai.commons.domain.ApiResponse;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import com.micro.ai.auth.annotation.AuditLogAnnotation;
import jakarta.validation.Valid;
import java.util.List;

/**
 * 字典类型控制器
 *
 * @author micro-ai
 * @since 2025-01-29
 */
@Slf4j
@RestController
@RequestMapping("/api/dict/types")
@Tag(name = "字典类型管理", description = "字典类型相关接口")
public class DictTypeController {

    @Autowired
    private DictTypeService dictTypeService;

    /**
     * 分页查询字典类型
     */
    @GetMapping("/page")
    @Operation(summary = "分页查询字典类型", description = "分页查询字典类型列表")
    @PreAuthorize("hasAuthority('dict:view')")
    public ApiResponse<IPage<DictType>> getDictTypePage(
            @Parameter(description = "页码") @RequestParam(defaultValue = "1") Integer current,
            @Parameter(description = "每页大小") @RequestParam(defaultValue = "10") Integer size,
            @Parameter(description = "字典类型编码") @RequestParam(required = false) String code,
            @Parameter(description = "字典类型名称") @RequestParam(required = false) String name,
            @Parameter(description = "状态") @RequestParam(required = false) String status) {
        
        String tenantId = "tn_system"; // 暂时使用系统租户
        Page<DictType> page = new Page<>(current, size);
        IPage<DictType> result = dictTypeService.getDictTypePage(page, code, name, status, tenantId);
        
        return ApiResponse.success(result);
    }

    /**
     * 查询所有启用的字典类型
     */
    @GetMapping("/active")
    @Operation(summary = "查询启用的字典类型", description = "查询所有启用的字典类型")
    @PreAuthorize("hasAuthority('dict:view')")
    public ApiResponse<List<DictType>> getActiveDictTypes() {
        String tenantId = "tn_system"; // 暂时使用系统租户
        List<DictType> result = dictTypeService.getActiveDictTypes(tenantId);
        
        return ApiResponse.success(result);
    }

    /**
     * 根据ID查询字典类型
     */
    @GetMapping("/{id}")
    @Operation(summary = "查询字典类型详情", description = "根据ID查询字典类型详情")
    @PreAuthorize("hasAuthority('dict:view')")
    public ApiResponse<DictType> getDictTypeById(@PathVariable String id) {
        DictType dictType = dictTypeService.getById(id);
        if (dictType == null) {
            return ApiResponse.error("A0001", "字典类型不存在");
        }
        
        return ApiResponse.success(dictType);
    }

    /**
     * 根据编码查询字典类型
     */
    @GetMapping("/code/{code}")
    @Operation(summary = "根据编码查询字典类型", description = "根据编码查询字典类型")
    @PreAuthorize("hasAuthority('dict:view')")
    public ApiResponse<DictType> getDictTypeByCode(@PathVariable String code) {
        String tenantId = "tn_system"; // 暂时使用系统租户
        DictType dictType = dictTypeService.getByCode(code, tenantId);
        
        return ApiResponse.success(dictType);
    }

    /**
     * 创建字典类型
     */
    @PostMapping
    @Operation(summary = "创建字典类型", description = "创建新的字典类型")
    @PreAuthorize("hasAuthority('dict:create')")
    @AuditLogAnnotation(resourceType = "DICT_TYPE", action = "CREATE")
    public ApiResponse<Boolean> createDictType(@Valid @RequestBody DictType dictType) {
        String tenantId = "tn_system"; // 暂时使用系统租户
        dictType.setTenantId(tenantId);
        
        boolean result = dictTypeService.createDictType(dictType);
        return ApiResponse.success(result);
    }

    /**
     * 更新字典类型
     */
    @PutMapping("/{id}")
    @Operation(summary = "更新字典类型", description = "更新字典类型信息")
    @PreAuthorize("hasAuthority('dict:update')")
    @AuditLogAnnotation(resourceType = "DICT_TYPE", action = "UPDATE")
    public ApiResponse<Boolean> updateDictType(@PathVariable String id, @Valid @RequestBody DictType dictType) {
        dictType.setId(id);
        boolean result = dictTypeService.updateDictType(dictType);
        
        return ApiResponse.success(result);
    }

    /**
     * 删除字典类型
     */
    @DeleteMapping("/{id}")
    @Operation(summary = "删除字典类型", description = "删除字典类型")
    @PreAuthorize("hasAuthority('dict:delete')")
    @AuditLogAnnotation(resourceType = "DICT_TYPE", action = "DELETE")
    public ApiResponse<Boolean> deleteDictType(@PathVariable String id) {
        boolean result = dictTypeService.deleteDictType(id);
        
        return ApiResponse.success(result);
    }

    /**
     * 批量删除字典类型
     */
    @DeleteMapping("/batch")
    @Operation(summary = "批量删除字典类型", description = "批量删除字典类型")
    @PreAuthorize("hasAuthority('dict:delete')")
    @AuditLogAnnotation(resourceType = "DICT_TYPE", action = "DELETE")
    public ApiResponse<Boolean> batchDeleteDictTypes(@RequestBody List<String> ids) {
        boolean result = dictTypeService.batchDeleteDictTypes(ids);
        
        return ApiResponse.success(result);
    }
}
